Milestone Project 2 - Blackjack Game

In this milestone project you will be creating a Complete BlackJack Card Game in Python. Here are the requirements: You need to create a simple text-based BlackJack game The game needs to have one player versus an automated dealer. The player can stand or hit. The player must be able to pick their betting amount. You need to keep track of the players total money. You need to alert the player of wins, losses, or busts, etc... And most importantly: You must use OOP and classes in some portion of your game. You can not just use functions in your game. Use classes to help you define the Deck and the Player's hand. There are many right ways to do this, so explore it well! Feel free to expand this game-try including multiple players. Try adding in Double-Down and card splits! Remember, you are free to use any resources you want and as always: HAVE FUN!

Blackjack in Python


To Do


  • required functions:
    • work is needed on def status_check(x) and blackjack functions
      • status_check could condense everything substantially by passing the person's name to the function
    • check to see if someone busts AND take some action
    • dealer will always stand at some value
    • check for ace, king, queen, jack and change number to 10
    • make ace be 11 or 1
    • error checking to determine if the input is correct
      • [x] check for card input
      • [x] check for number input
    • [x] deal cards to each player
    • [x] initial dealing of card
    • [x] determine if to hit or stand
    • [x] determine count of a players cards
    • [x] add error proofing at all user inputs
    • [x] randomly generate a deck of cards and shuffle it
    • [x] add functionality to anteUp function to verify the player has sufficient funds
    • [x] determine how much someone is going to be bet
    • [x] test to see if the person wants to continue playing
    • [x] shuffle deck
  • classes
    • player
      • [x] bank credit
      • [x] m/f
      • [x] name
      • [x] the cards in their hand
    • deck of cards
      • suits
      • cards within suit

Game Play


  • (develop the game as 1 player, then player and dealer with all cards show, and then dealer's cards hidden)
  • display the rules of blackjack
  • determine how much someone will bet
  • suffle cards
  • deal out cards
  • dealer: show 1 card and hide the others
  • player: show all cards
  • ask if player wants to hit or stand
  • give total of player's cards
  • determine if anyone has busted
  • determine if someone has 21
  • determine if it's a tie (dealer wins???)
  • once everyone stands, determine who wins
  • keep playing???
  • card representation: ASCII art / simple text output / grid output

In [35]:
# modules to import
import random
from IPython.display import clear_output

In [36]:
# class for each blackjack player
class Gambler(object):
    def __init__(self,name='Player 1',bank=100,sex='M',hand=[],bet=5):
        self.name = name
        self.bank = bank
        self.sex = sex
        self.hand = hand
        self.bet = bet
        return
    
    def setName(self):
        self.name = input('Enter your name: ')
        return
    
    def anteUp(self):
        if self.bank == 0:
            return 1
        
        self.bet = input('Your bank balance is %s. Enter your bet: ' %self.bank)
        self.bet = int_check(self.bet)

        while self.bet > self.bank:
            print('bet is too large. you have insufficient funds.')
            return self.anteUp()
        
        while self.bet < 1:
            print('bet must be greater than 0')
            return self.anteUp()
        
        self.bank -= self.bet
        return
    
    def hand_status(self):
        print(self.name,'Hand:',self.hand,'Current Sum:',self.current_sum())

    def current_sum(self):
        y = 0
        
        for i in self.hand:
            z = str(i[:-1])
            ace = 0

            if z == 'J' or z == 'Q' or z == 'K':
                z = 10
                y += z
            elif z == 'A':
                ace = 1
                z = 11
                y += int(z)
            else:
                y += int(z)
            
        x = ace_test(y,ace)
        return x
    
    def win(self):
        self.bank += self.bet*2
        return
    
    def tie(self):
        self.bank += self.bet
        return
    

# class for the deck
class Deck(object):
    def __init__(self):
        self.spades = ['2\u2660','3\u2660','4\u2660','5\u2660','6\u2660','7\u2660','8\u2660','9\u2660','10\u2660','J\u2660','Q\u2660','K\u2660','A\u2660']
        self.hearts = ['2\u2665','3\u2665','4\u2665','5\u2665','6\u2665','7\u2665','8\u2665','9\u2665','10\u2665','J\u2665','Q\u2665','K\u2665','A\u2665']
        self.diamonds = ['2\u2666','3\u2666','4\u2666','5\u2666','6\u2666','7\u2666','8\u2666','9\u2666','10\u2666','J\u2666','Q\u2666','K\u2666','A\u2666']
        self.clubs = ['2\u2663','3\u2663','4\u2663','5\u2663','6\u2663','7\u2663','8\u2663','9\u2663','10\u2663','J\u2663','Q\u2663','K\u2663','A\u2663']
        self.full_deck = self.spades + self.hearts + self.diamonds + self.clubs
        return
    
    def shuffle(self):
        random.shuffle(self.full_deck)
        return
    
    def next_card(self):
        return self.full_deck.pop()

In [37]:
# error proofing and quitting funcitons
def keep_playing(str_text):
    i = 1
    while i == 1:
        i = 0
        continue_playing = input(str_text)
        if continue_playing == 'Y':
            return 1
        elif continue_playing == 'N':
            return 0
        else:
            print('invalid entry. enter only Y or N')
            i = 1

def int_check(val):
    while True:
        try:
            val = int(val)
            return val
        except:
            print('That\'s not a number!')
            val = input("Please enter a number: ")
            continue
        else:
            break

def ace_test(y,ace):
    if y > 21 and ace == 1:
        return y-10
    else:
        return y

def status_check(x):
    if x > 21:
        return 0
    elif x == 21:
        return 1
    else:
        return 2

In [40]:
def blackjack():
    for num in range(2):
        deal_to_player()
        deal_to_dealer()

    player.hand_status()
    dealer.hand_status()
    
    if status_check(player.current_sum()) == 1:
        print('blackjack! you win!')
        player.bank += player.bet * 2
        return

    if status_check(dealer.current_sum()) == 1:
        print('blackjack! dealer wins!')
        return

    while 1:
        if keep_playing('Hit? (Y/N): ') == 1:
            deal_to_player()
            
        if status_check(player.current_sum()) == 0:
            print('you busted!')
            break
        elif status_check(player.current_sum()) == 1:
            print('blackjack! you win!')
            player.bank += player.bet * 2
            break

            
        # deal to the dealer and check if bust
        if dealer.current_sum() < 17:
            deal_to_dealer()
        dealer.hand_status()
        if dealer.current_sum() > 21:
            print('dealer busted. you win!')
            break
        elif dealer.current_sum() == 21:
            print('blackjack! dealer win!')
            break
        else:
            continue

# check to see if anyone has a blackjack

# check to see if player wants to hit
# check to see if plaer has a blackjack
# check to see if player busts

# check to see if delaer wants to hit
# check to see if dealer has a blackjack
# check to see if dealer busts

# reprint hand status

    return

def deal_to_player():
    x = cards.full_deck.pop()
    player.hand.append(x)
    return

def deal_to_dealer():
    x = cards.full_deck.pop()
    dealer.hand.append(x)
    return

In [41]:
# initialize deck and shuffle it
cards = Deck()
cards.shuffle()

# initialize the player and dealer
player = Gambler('Player1',bank=100,sex='M',hand=[])
player.setName()
dealer = Gambler('Dealer',bank=1000000,sex='M',hand=[])
    
# actual loop for the program
while 1:

    # Burnside Blackjack
    print('Welcome to the Burnside Casino. It\'s time for Blackjack!')

    broke = player.anteUp()
    if broke == 1:
        print('you loser. you are broke! you are kicked out of the casino!')
        break
    
    blackjack()

    # test to see if we should continue playing
    if keep_playing('Continue playing? (Y/N): ') == 1:
        clear_output()
        player.hand = []
        dealer.hand = []
        cards = Deck()
        cards.shuffle()
        print(cards.full_deck)
        continue
    else:
        # clear_output()
        print('thanks for playing')
        break


Enter your name: rich
Welcome to the Burnside Casino. It's time for Blackjack!
Your bank balance is 100. Enter your bet: 1
rich Hand: ['9♥', 'K♠'] Current Sum: 19
Dealer Hand: ['7♠', 'K♣'] Current Sum: 17
Hit? (Y/N): Y
you busted!
Continue playing? (Y/N): N
thanks for playing

In [ ]: